package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import org.come.Frame.BoothBoxJframe;
import org.come.Frame.TradeJframe;
import org.come.Frame.ZhuFrame;
import org.come.baitan.BaiTanJframe;
import org.come.entity.RoleSummoning;
import org.come.mouslisten.PetAddPointMouslisten;
import org.come.mouslisten.TradeChoseGoodsMouslisten;
import org.come.until.*;

import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.goodbtn;
import com.tool.role.RoleData;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;

 

/**
 * @author 郑天宇
 * @date : 2017年12月05日 下午05:00:00
 */

public class TradeJpanel extends JPanel {

    private TradeJframe tradejframe;
    private JLabel labtradepet;// 放置要交易的宠物的图片
    private JList<String> listEquipment;// 交易召唤兽列表
    private DefaultListModel<String> modelTrade, modelname;
    private Color fontcolor;// 列表中对象的名字颜色
    private JScrollPane jScrollPane1;// 滚动条
    private JButton btnright1, btnright2, btnright3, btnright4, btnright5;// 物品栏里面的六个按钮
    // 放置6个背包切换按钮
    private goodbtn[] btnrights;
    private JLabel[] GoodsListLabel = new JLabel[24];// 用来存放24个物品表格

    public TradeJpanel(TradeJframe tradejframe) throws Exception {
    		this.tradejframe = tradejframe;
            this.setPreferredSize(new Dimension(409, 436));
            this.setLayout(null);
            this.setOpaque(false);
            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 15);
            offBtn.setBounds(409 - 37, 9, 25, 25);
            this.add(offBtn);
            // 放置要交易的宠物的图片
            labtradepet = new JLabel();
            labtradepet.setBounds(189 + 25, 37 - 10, 146 + 17, 170);
            this.add(labtradepet);
            // 交易召唤兽列表对象
            modelname = new DefaultListModel<String>();
            // 交易召唤兽列表
            listEquipment = new JList<String>();
            listEquipment.setOpaque(false);
            listEquipment.setSelectionBackground(new Color(33, 42, 52));
            fontcolor = Color.GREEN;
            listEquipment.setSelectionForeground(fontcolor);
            listEquipment.setForeground(fontcolor);
            listEquipment.setFont(new Font("微软雅黑", 1, 14));
            listEquipment.setBackground(UIUtils.Color_BACK); // 设置列表框为透明背景
            listEquipment.setModel(modelname);
            listEquipment.addMouseListener(new MouseListener() {
                @Override
                public void mouseReleased(MouseEvent e) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void mousePressed(MouseEvent e) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void mouseExited(MouseEvent e) {

                }

                @Override
                public void mouseEntered(MouseEvent e) {

                }

                @Override
                public void mouseClicked(MouseEvent e) {
                    if (listEquipment.getSelectedIndex() != -1) {
                        int index = listEquipment.getSelectedIndex();
                        // 判断模式是否为空
                        RoleSummoning pet = UserMessUntil.getPetListTable().get(index);

//                        if (!pet.getSsn().equals("2") || !pet.getSsn().equals("3") || !pet.getSsn().equals("4")) {
                            if ("0".equals(pet.getQuality())|| "1".equals(pet.getQuality())  || "3".equals(pet.getQuality()) || "4".equals(pet.getQuality())) {
                            ZhuFrame.getZhuJpanel().addPrompt2("召唤兽" + pet.getSummoningname() + "禁交易！！");
                               return;
                             }

                        // 先判断召唤兽是否有加锁
                        if (pet.getPetlock() == 1) {
                            ZhuFrame.getZhuJpanel().addPrompt("召唤兽" + pet.getSummoningname() + "已被加锁，不可摆摊！！");
                            return;
                        }
                        // 先判断是否有参战的召唤兽
                        if (RoleData.getRoleData().getLoginResult().getSummoning_id() != null) {// 有
                            if (pet.getSid().compareTo(RoleData.getRoleData().getLoginResult().getSummoning_id()) == 0) {
                                // 提示信息
                                ZhuFrame.getZhuJpanel().addPrompt2("该召唤兽在参战中，不可摆摊！");
                                return;
                            }
                        }
                        if (ZhuJpanel.getPetMount(pet.getSid()) != null) {
                            ZhuFrame.getZhuJpanel().addPrompt2("这只召唤兽被管制中，不可摆摊！！！");
                            return;
                        }
                        BoothBoxJframe.getBoothboxjframe().getBoothboxjpanel().XZBuy(null, pet);
                        BaiTanJframe.getBaiTanJframe().getBaiTanJpanel().XZBuy(pet);
                        PetAddPointMouslisten.showPetValue();
                    }
                }
            });
            // 交易召唤兽列表滚动条
            jScrollPane1 = new JScrollPane(listEquipment);
            jScrollPane1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            jScrollPane1.getVerticalScrollBar().setUI(new SrcollPanelUI());
            jScrollPane1.getViewport().setOpaque(false);
            jScrollPane1.setOpaque(false);
            jScrollPane1.setBounds(23 + 23, 42, 139 + 14, 117);
            jScrollPane1.setBorder(null);
            jScrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            this.add(jScrollPane1);

            // 存放物品的表格1~24
            for (int i = 0; i < 24; i++) {
                GoodsListLabel[i] = new JLabel();
                GoodsListLabel[i].addMouseListener(new TradeChoseGoodsMouslisten(i));
                GoodsListLabel[i].setBounds(19 + 27 + i % 6 * 51, 212 - 13 + i / 6 * 51, 49, 49);
                this.add(GoodsListLabel[i]);
            }
            btnrights = new goodbtn[6];
            for (int i = 0; i < btnrights.length; i++) {
                btnrights[i] = new goodbtn("skin/8100226/newbutton/C0"+(i+1)+".png", 0, this, i);
                btnrights[i].setBounds(325 + 28, 198 + i * 35, 35, 31);
                this.add(btnrights[i]);
            }
    }

    ImageIcon icon;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        	if (icon == null) {
                icon = new ImageIcon("inkImg/newbackground/B251.png");
            }
            // 道具栏
            g.drawImage(icon.getImage(), 0, 0, 409, 436, this);
            g.setColor(Color.white);
            g.setFont(new Font("微软雅黑", 0, 12));
            Util.drawMoney(g, 62 + 24, 183);
            GoodsListFromServerUntil.draw(g, 19 + 27, 212 - 13);
    }

    public goodbtn[] getBtnrights() {
        return btnrights;
    }

    public void setBtnrights(goodbtn[] btnrights) {
        this.btnrights = btnrights;
    }

    public TradeJframe getTradejframe() {
        return tradejframe;
    }

    public void setTradejframe(TradeJframe tradejframe) {
        this.tradejframe = tradejframe;
    }

    public JLabel getLabtradepet() {
        return labtradepet;
    }

    public void setLabtradepet(JLabel labtradepet) {
        this.labtradepet = labtradepet;
    }

    public JList<String> getListEquipment() {
        return listEquipment;
    }

    public void setListEquipment(JList<String> listEquipment) {
        this.listEquipment = listEquipment;
    }

    public DefaultListModel<String> getModelTrade() {
        return modelTrade;
    }

    public void setModelTrade(DefaultListModel<String> modelTrade) {
        this.modelTrade = modelTrade;
    }

    public DefaultListModel<String> getModelname() {
        return modelname;
    }

    public void setModelname(DefaultListModel<String> modelname) {
        this.modelname = modelname;
    }

    public Color getFontcolor() {
        return fontcolor;
    }

    public void setFontcolor(Color fontcolor) {
        this.fontcolor = fontcolor;
    }

    public JScrollPane getjScrollPane1() {
        return jScrollPane1;
    }

    public void setjScrollPane1(JScrollPane jScrollPane1) {
        this.jScrollPane1 = jScrollPane1;
    }

    public JButton getBtnright1() {
        return btnright1;
    }

    public void setBtnright1(JButton btnright1) {
        this.btnright1 = btnright1;
    }

    public JButton getBtnright2() {
        return btnright2;
    }

    public void setBtnright2(JButton btnright2) {
        this.btnright2 = btnright2;
    }

    public JButton getBtnright3() {
        return btnright3;
    }

    public void setBtnright3(JButton btnright3) {
        this.btnright3 = btnright3;
    }

    public JButton getBtnright4() {
        return btnright4;
    }

    public void setBtnright4(JButton btnright4) {
        this.btnright4 = btnright4;
    }
    public JButton getBtnright5() {
        return btnright5;
    }

    public void setBtnright5(JButton btnright5) {
        this.btnright5 = btnright5;
    }

    public JLabel[] getGoodsListLabel() {
        return GoodsListLabel;
    }

    public void setGoodsListLabel(JLabel[] goodsListLabel) {
        GoodsListLabel = goodsListLabel;
    }
    
}
